﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using System.Collections.ObjectModel;
using AnesSystem.Services;
using System.Collections;

namespace AnesSystem.ViewModels
{
    public class ConfigAnesMethodCtrlViewModel : BaseViewModel<ConfigAnesMethodCtrlViewModel>
    {
        #region Variables
        private ObservableCollection<ConfigAnesMethodsModel> _configAnesMethodList;
        private ConfigAnesMethodsModel _configAnesMethodsModel;

        #endregion

        #region Constructor
        public ConfigAnesMethodCtrlViewModel()
        {
            ConfigAnesMethodList = new ObservableCollection<ConfigAnesMethodsModel>();
            InitData();
           
        }

        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {

            ConfigAnesMethodsModel configAnesMethodsModel = sender as ConfigAnesMethodsModel;
            ConfigAnesMethodService.Instance.AddUpdate(configAnesMethodsModel);
        }



        #endregion

        #region Properties
        public ObservableCollection<ConfigAnesMethodsModel> ConfigAnesMethodList
        {
            get
            {
                return _configAnesMethodList;
            }

            set
            {
                _configAnesMethodList = value;
            }
        }



        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void InitData()
        {
            IEnumerable<ConfigAnesMethodsModel> list = ConfigAnesMethodService.Instance.FindAll();

            foreach (var item in list)
            {
                item.PropertyChanged += Item_PropertyChanged;
                ConfigAnesMethodList.Add(item);
            }
        }

  

        #endregion

        #region Public/Protected Methods
        public void addConfigAnesMethod()
        {
            _configAnesMethodsModel = new ConfigAnesMethodsModel();
            _configAnesMethodsModel.Name = "请编辑";
            _configAnesMethodsModel.Deleted = false;
            _configAnesMethodsModel.PropertyChanged += Item_PropertyChanged;
            _configAnesMethodList.Add(_configAnesMethodsModel);

        }
        public void save2DB()
        {
            _configAnesMethodsModel.Code = "code_" + _configAnesMethodsModel.Index;

            ConfigAnesMethodService.Instance.AddUpdate(_configAnesMethodsModel);

        }
        public void refresh() {
            _configAnesMethodList.Clear();
            InitData();
        }



        #endregion

        #region EventHandlers

        #endregion

    }
}
